<?php
namespace Admin\Model;
use Admin\Model\BaseModel;
class AdminModel extends BaseModel
{
    public $tableName = 'admin';
    public $_validate = array(
        array('username', '1,100', '标题必须在1~100之间', 0, 'length', 3),
        array('realname', '0,100', '真实姓名必须在1~100之间', 0, 'length', 3),
        array('username', '', '用户名已存在', 0, 'unique', 3),
        array('content', '0,255', '内容信息必须在0~255之间', 0, 'length', 3)
    );

    public function getAdminInfoByUserName($userName)
    {
        return $this->where("username = '$userName' AND dostatus = 1")->find();
    }

    /**
     * 通过id取用户详情
     * @param int $uid
     * @return array
     */
    public function getAdminInfoById($uid)
    {
        $userInfo = $this->where(array('id' => $uid))->find();
        if (false === $userInfo) {
            return array();
        }
        return $userInfo;
    }

    public function saveNewPassWord($uid, $passWord)
    {
        $data['id'] = $uid;
        $data['password'] = $passWord;
        $data['update_time'] = date("Y-m-d H:i:s");
        return $this->save($data);
    }

    /**
     * 用户分页列表
     *
     */
    public function getNewsPageList($where, $order = '', $nowPage = 1, $pageSize = 10)
    {
        $count = $this->where($where)->count();
        $Page = new \Admin\Util\Page($count, $pageSize);
        $data['list'] = $this->where($where)->order($order)->page($nowPage . ',' . $Page->listRows)->select();
        $data['page'] = $Page->show();
        return $data;
    }

    /**
     * 保存数据
     * @param type $dataArray
     * @return type
     * @throws Exception
     */
    public function doSave($dataArray)
    {
        if (empty($dataArray['id'])) {
            $dataArray['create_time'] = date("Y-m-d H:i:s");
        }
        $dataArray['update_time'] = date("Y-m-d H:i:s");

        if (false === $this->create($dataArray)) {
            throw new \Exception($this->getError());
        }

        if (empty($dataArray['id'])) {
            $res = $this->add($dataArray);
        } else {
            $res = $this->save($dataArray);
        }
        return $res;
    }

    /**
     * 得到uid的权限id数组
     * @param int $uid
     * @param string $class_id
     * @return array
     */
    public function getAtomArray4id($uid, $class_id = '')
    {
        $searchClassId = '';
        if (!empty($class_id)) {
            $searchClassId = $class_id . '%';
        }
        $permissionAtomObj = new \Admin\Model\AdminAtomModel();
        #超级管理员,返回所有权限
        if ($uid == 1) {
            #查询所有的权限
            $atomList = $permissionAtomObj->getAtomList4MenuID($searchClassId);
            $atomArray = key2index($atomList, 'id');
            return $atomArray;
        }

        $atomList = '';
        $userInfo = $this->getAdminInfoById($uid);
        #通过uid找到用户组id
        $groupId = $userInfo['group_id'];
        $userGroupObj = new \Admin\Model\AdminGroupModel();
        $groupInfo = $userGroupObj->getUserGroupInfo($groupId);
        #通过用户组id找到角色id列表
        $roleList = $groupInfo['role_list'];
        $roleArray = explode(',', $roleList);
        $userRoleObj = new \Admin\Model\AdminRoleModel();
        #通过角色id列表，找到权限id
        foreach ($roleArray as $key => $value) {
            $roleInfo = $userRoleObj->getUserRoleInfo($value);
            $atomList .= $roleInfo['atom_list'] . ',';
        }
        $atomArray = array_flip(explode(',', substr($atomList, 0, -1)));
        //通过权限id获取完整的权限信息
        foreach ($atomArray as $key => $value) {
            $atomArray[$key] = $permissionAtomObj->getPermissionAtomInfo($key);
        }
        return $atomArray;
    }

    /**
     * 删除用户
     * @param type $id
     * @return type
     */
    public function delAdmin($id)
    {
        $where['id'] = $id;
        $res = $this->where($where)->delete();
        return $res;
    }

    protected function _after_update($data, $options)
    {

    }
}

?>